package JDBCPre;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Scanner;

public class TestCode {
    public static void main(String[] args) throws SQLException {
//        findall();
//       insert();
//        update();
//        delete();
        deluser();
    }

    //1.查询
    public static void findall() throws SQLException {
        JDBCPreLearn1 jdbcPreLearn1 = new JDBCPreLearn1();
        ArrayList<User> all = jdbcPreLearn1.findAll();
        System.out.println("all = " + all);
    }

    //    2.插入
    public static void insert() throws SQLException {
        JDBCPreLearn1 jdbcPreLearn1 = new JDBCPreLearn1();
        User user = new User();

        Scanner scanner = new Scanner(System.in);
        System.out.println("id");
        user.setId(scanner.nextInt());
        System.out.println("name");
        user.setName(scanner.next());
        System.out.println("password");
        user.setPassword(scanner.next());
        System.out.println("nickname");
        user.setNickname(scanner.next());
        jdbcPreLearn1.insert(user);

    }

    //    3.删除
    public static void delete() throws SQLException {
        JDBCPreLearn1 jdbcPreLearn1 = new JDBCPreLearn1();
        User user = new User();
        Scanner scanner = new Scanner(System.in);
        System.out.println("id");
        user.setId(scanner.nextInt());
        jdbcPreLearn1.delete(user);

    }

    //    4.修改
    public static void update() throws SQLException {
        JDBCPreLearn1 jdbcPreLearn1 = new JDBCPreLearn1();
        User user = new User();

        Scanner scanner = new Scanner(System.in);
        System.out.println("oldid");
        int id = scanner.nextInt();
        User old = jdbcPreLearn1.findall1(id);
        System.out.println("old = " + old);
        scanner.nextLine();

        System.out.println("name");
        String s = scanner.nextLine();
        if (s.equals("")) {
            user.setName(old.getName());
        } else {
            user.setName(scanner.next());
        }

        System.out.println("password");
        String s1 = scanner.nextLine();
        if (s1.equals("")) {
            user.setPassword(old.getPassword());
        } else {
            user.setName(scanner.next());
        }

        System.out.println("nickname");
        String s2 = scanner.nextLine();
        if (s2.equals("")) {
            user.setNickname(old.getNickname());
        } else {
            user.setName(scanner.next());
        }


        jdbcPreLearn1.update(user, id);

    }
    public static void deluser() throws SQLException {
        JDBCPreLearn1 jdbcPreLearn1 = new JDBCPreLearn1();
        User user = new User();
        Scanner scanner = new Scanner(System.in);
        System.out.println("id");
        user.setId(scanner.nextInt());
        jdbcPreLearn1.delUSer1(scanner.nextInt());

    }

}
